import time

from selenium.webdriver.common.by import By

from Pages import BasePage

class OBTFlightDetailPage(BasePage.Action):

    def __init__(self):
        #申请改期
        self.applyChange_xpath = (By.XPATH, "//span[@class='change']")
        #申请退票
        self.applyRefund_xpath = (By.XPATH, "//span[@class='refund']")
        #选择航段去改期
        self.flightSegment_change_xpath = (By.XPATH, "//span[@class='el-checkbox__inner']")
        #选择航段去退票
        self.flightSegment_refund_xpath = (By.XPATH, "//div[@class='block airline']//span[@class='el-checkbox__inner']")
        #改期日期
        self.changeDate_xpath = (By.XPATH, "//input[@class='form-control datepicker-input  input-hasicon']")

        #当前日期
        self.originDate = (By.XPATH, "//span[contains(@class, 'datepicker-dateRange-item-active')]")

        #自愿改期
        self.selfWillingChange_xpath = (By.XPATH, "//dt[text()='自愿改期']")
        #自愿退票
        self.selfWillingRefund_xpath = (By.XPATH, "//dl/dt[text()='自愿退票']")

        #改期原因下拉选择框
        self.changeReasons_xpath = (By.XPATH, "//input[@placeholder='请选择改期原因']")
        #改期原因备注
        self.changeReason1_xpath = (By.XPATH, "//li/span[text()='我是自愿改签的']")

        #去选择改期航班按钮
        self.flightsChooseButton_xpath = (By.XPATH, "//span[text()='去选择改期航班']")
        #确认按钮
        self.sure_xpath = (By.XPATH, "//button/span[contains(.,'确认')]")
        #提交改期按钮
        self.submitChangeButton_xpath = (By.XPATH, "//span[text()='提交改期']")
        #提交退票申请
        self.submitRefundButton_xpath = (By.XPATH, "//span[text()='提交退票申请']")
        #LL提交退票按钮
        self.submitRefundButton_xpath1 = (By.XPATH, "//div[@class='foot-btn']//span")
        #确认提交按钮
        self.ensureSubmit_xpath = (By.XPATH, "//span[text()='确认提交']")
        #
        self.copyOrderNo_xpath = (By.XPATH, "//span[text()='复制']")

    def getOrderNo(self):
        time.sleep(2)
        orderNo = self.find_element(*self.copyOrderNo_xpath).get_attribute("data-clipboard-text")
        return orderNo
        time.sleep(2)


    def startChange(self):
        #默认全选所有可选乘客，直接申请改期
        time.sleep(2)
        self.smart_click(*self.applyChange_xpath)
        time.sleep(6)
        self.find_elements(*self.flightSegment_change_xpath)[0].click()
        #    print(self.originDate)
        time.sleep(6)
        self.find_elements(*self.changeDate_xpath)[0].click()
        time.sleep(1)
        #改期航班日期,设为当前航班起飞时间后的7天
        originDate = self.find_elements(*self.originDate)[0].get_attribute('data-date')
        changeDate = time.strftime('%Y-%m-%d', time.localtime(time.mktime(time.strptime(originDate, '%Y-%m-%d'))+604800))
        self.datePlaneChangeDay_xpath = (By.XPATH, "//span[@data-date='%s' and (contains(@class,'day-cell') and not(contains(@class,'datepicker-item-disabled')))]" % changeDate)

        self.find_elements(*self.datePlaneChangeDay_xpath)[0].click()
        time.sleep(1)
        self.find_elements(*self.selfWillingChange_xpath)[0].click()
        time.sleep(1)
        self.find_elements(*self.changeReasons_xpath)[0].click()
        time.sleep(1)
        self.find_elements(*self.changeReason1_xpath)[0].click()
        time.sleep(1)
        self.find_elements(*self.flightsChooseButton_xpath)[0].click()
        time.sleep(3)
        self.find_elements(*self.sure_xpath)[0].click()
        time.sleep(3)

    def submitChange(self):
        time.sleep(3)
        self.smart_click(*self.submitChangeButton_xpath)
        time.sleep(3)
        self.smart_click(*self.ensureSubmit_xpath)
        time.sleep(5)

    def applyTicketRefund(self):
        self.smart_click(*self.applyRefund_xpath)
        time.sleep(2)
        self.find_elements(*self.flightSegment_refund_xpath)[0].click()
        time.sleep(1)
        self.smart_click(*self.selfWillingRefund_xpath)
        time.sleep(1)
        self.smart_click(*self.submitRefundButton_xpath1)
        time.sleep(1)
        self.smart_click(*self.ensureSubmit_xpath)
        time.sleep(5)
